﻿CREATE TRIGGER yaf_UserGroup_delete
ON dbo.yaf_UserGroup
FOR DELETE
AS
    BEGIN
        DECLARE  @UserID INT
        DECLARE  @BoardID INT
        DECLARE  @GroupID INT
        DECLARE  @Flags INT
        DECLARE deleted_cursor CURSOR  FOR
        SELECT a.UserID,
               b.BoardID,
               b.GroupID,
               b.Flags
        FROM   deleted a
               JOIN dbo.yaf_Group b
                 ON b.GroupID = a.GroupID
        OPEN deleted_cursor
        FETCH NEXT FROM deleted_cursor
        INTO @UserID,
             @BoardID,
             @GroupID,
             @Flags
        WHILE @@FETCH_STATUS = 0
        BEGIN
            IF (@Flags & 2) <> 0
            BEGIN
                -- This is the guest group. We can't remove users from the guest group.
                RAISERROR ('Users can not be removed from the guest group',16,1)
            END
            FETCH NEXT FROM deleted_cursor
            INTO @UserID,
                 @BoardID,
                 @GroupID,
                 @Flags
        END
        CLOSE deleted_cursor
        DEALLOCATE deleted_cursor
    END


